<?php

/*
 * @copyright :     Ienter asia Technologies
 * @author :        Quyet
 * @version         v.1.0
 * @created         20/6/2014
 * @Description :   Model salaryinfo 
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mmod_salaryinfo extends MYAD_Model {

    protected $table_name = 'm_employeesalaryinfo';
    protected $table_employee = 'm_employee';

    function __construct() {
        parent::__construct();
    }

    /*
     * @author :        Quyet
     * @created:        20/6/2014
     * @Description:    Get all data for salary info
     * @Input :         (Integer) $employeeID
     *                  (Integer) $page
     * @Output :        (Array) data for view data
     */

    function getAllData($employeeID = 0, $page = 0) {
        $conditionEmployee = array(
            'where' => array(
                'EmployeeId' => $employeeID,
                'CompanyCode' => $this->session->userdata('CompanyCode')
            ),
            'first' => TRUE
        );
        $employee = $this->selectDB($this->table_employee, $conditionEmployee);
        if (!$employee) {
            redirect('/employee');
        }

        /* load all label */
        $labelData = $this->getLabelData();

        /* load all employee data range */
        $allData = $this->countResult($this->table_name, array('EmployeeId' => $employeeID));

        /* load pagination */
        $pagiData = array(
            'base_url' => base_url('employee/salary-info/'),
            'total_rows' => $allData,
            'uri_segment' => 3,
        );
        $pagination = $this->config->item('pagination');
        $offset = isset($pagiData['per_page']) ? $pagiData['per_page'] : $pagination['per_page'];
        $pagi = $this->getPagination($pagiData);

        /* load salaryinfo data range */
        $condition = array(
            'where' => array(
                'EmployeeId' => $employeeID,
            ),
            'limit' => array($page, $offset),
            'order_by' => 'ApplyDate DESC'
        );
        $salaryData = $this->getSalaryinfor($condition);

        $data = array(
            'show' => 'salaryinfolist',
            'pagi' => $pagi,
            'nullcharacter' => $this->lang->line('nullcharacter'),
            'message' => $this->lang->line('emp_sli_table_empty_data'),
            'order' => $page,
            'employeeID' => $employeeID,
            'employeeName' => $employee->EmployeeName,
        );
        return array_merge($data, $labelData, $salaryData);
    }

    /*
     * @author :        Quyet
     * @created:        20/6/2014
     * @Description:    Get label data of salary info
     * @Input :         void
     * @Output :        (Array) data label
     */

    function getLabelData() {

        $title = $this->lang->line('emp_sli_title');
        $lblemployeeName = $this->lang->line('emp_sli_employee_name');
        $lblnewRegister = $this->lang->line('emp_sli_new_register');
        $lblOrder = $this->lang->line('emp_sli_table_col_order');
        $lblGrossSalary = $this->lang->line('emp_sli_table_col_gross_salary');
        $lblApplyDate = $this->lang->line('emp_sli_table_col_apply_date');
        $lblEdit = $this->lang->line('emp_sli_table_col_edit');
        $lblActive = $this->lang->line('po_table_col_Active');

        $data = array(
            'title' => (empty($title) ? 'Employee Salary Information' : $title),
            'lblemployeeName' => (empty($lblemployeeName) ? 'Employee name' : $lblemployeeName),
            'lblnewRegister' => (empty($lblnewRegister) ? 'New register' : $lblnewRegister),
            'lblOrder' => (empty($lblOrder) ? 'No.' : $lblOrder),
            'lblGrossSalary' => (empty($lblGrossSalary) ? 'Gross Salary' : $lblGrossSalary),
            'lblApplyDate' => (empty($lblApplyDate) ? 'Apply Date' : $lblApplyDate),
            'lblEdit' => (empty($lblEdit) ? 'Edit' : $lblEdit),
            'lblActive' => (empty($lblActive) ? 'Active' : $lblActive),
        );
        return $data;
    }

    /*
     * @author :        Quyet
     * @created:        20/6/2014
     * @Description:    Get salary info by condition
     * @Input :         
     *                  (Array) $condition
     * @Output :        Array data /FALSE
     */

    function getSalaryinfor($condition = array()) {
        /* get salaryinfo */
        $temp = $this->selectDB($this->table_name, $condition);
        return array('data' => $temp);
    }

    /*
     * @author :        Quyet
     * @created:        20/6/2014
     * @Description:    check validate form add/edit employee
     * @Input :         void
     * @Output :        boolean
     */

    function validate() {
        /* convert */
        $_POST['txtGrossSalary'] = mb_convert_kana($_POST['txtGrossSalary'], 'n');
        $_POST['txtApplyDate'] = mb_convert_kana($_POST['txtApplyDate'], 'a');

        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('txtGrossSalary', $this->lang->line('emp_sli_update_lblGrossSalary'), 'required|greater_than[0]|less_than[100000]');
        $this->form_validation->set_rules('txtApplyDate', $this->lang->line('emp_sli_update_lblApplyDate'), 'required|max_length[7]|callback_validate_month');
        $this->form_validation->set_rules('txtDelete', $this->lang->line('po_update_lblDelete'), 'max_length[1]');
        return $this->form_validation->run();
    }

    /*
     * @author :        Quyet
     * @created:        20/6/2014
     * @Description:    update employee
     * @Input :         (Integer) $employeeID
     *                  (Integer) $salaryInfoId
     * @Output :        boolean
     */

    function updateSalaryInfo($employeeID = 0, $salaryInfoId = 0) {
        $applyDate = strtotime($this->input->post('txtApplyDate') . '/1');
        $conditionDuplicate = array(
            'SalaryInfoId !=' => $salaryInfoId,
            'EmployeeId' => $employeeID,
            'Applydate' => date('Y-m-01', $applyDate),
            'Del_Flag' => FALSE,
        );
        $duplicate = $this->countResult($this->table_name, $conditionDuplicate);
        if ($duplicate) {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('emp_sli_table_col_apply_date') . ': ' . $this->lang->line('emp_updateDB_duplicate')
            );
        }
        $data = array(
            'EmployeeId' => $employeeID,
            'GrossSalary' => $this->input->post('txtGrossSalary'),
            'ApplyDate' => date('Y-m-01', $applyDate),
            'Del_Flag' => $this->input->post('txtDelete'),
        );
        $condition = array();
        if ($salaryInfoId) {
            $condition['SalaryInfoId'] = $salaryInfoId;
        }
        $temp = $this->updateDB($this->table_name, $data, $condition);
        if ($temp) {
            return array(
                'flag' => TRUE,
                'message' => $this->lang->line('emp_updateDB_success')
            );
        } else {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('emp_updateDB_fall')
            );
        }
    }

    /*
     * @author :        Quyet
     * @created:        23/6/2014
     * @Description:    basic data for new add salary info
     * @Input :         (Interger) $employeeID
     * @Input :         (Interger) $salaryInfoId
     * @Output :        Array
     */

    function getAddSalaryInfo($employeeID = 0, $salaryInfoId = 0) {
        /* get language info */
        $title = ($salaryInfoId) ? $this->lang->line('emp_sli_update_title') : $this->lang->line('emp_sli_add_title');
        $dialogTitle = $this->lang->line('emp_dialog_title');
        $lblEmployeeName = $this->lang->line('emp_sli_update_lblEmployeeName');
        $lblGrossSalary = $this->lang->line('emp_sli_update_lblGrossSalary');
        $lblUSD = $this->lang->line('emp_sli_update_lblUSD');
        $lblApplyDate = $this->lang->line('emp_sli_update_lblApplyDate');
        $lblDelete = $this->lang->line('po_update_lblDelete');
        $lblSubmit = $this->lang->line('emp_sli_update_lblsubmit');
        $back = $this->lang->line('emp_add_btnBack');

        /* get employee */
        $condition = array(
            'where' => array('EmployeeId' => $employeeID),
            'first' => TRUE
        );
        $employee = $this->selectDB($this->table_employee, $condition);
        /* get employee salary info */
        $salaryInfo = $this->getSalaryInforByID($employeeID, $salaryInfoId);
        if (!isset($_POST['btnSubmit']) && $salaryInfo) {
            /* is load default */
            $emp = array(
                'txtGrossSalary' => $salaryInfo->GrossSalary,
                'txtApplyDate' => $this->setDate($salaryInfo->ApplyDate, 'Y/m'),
                'txtDateYear' => $this->setDate($salaryInfo->ApplyDate, 'Y'),
                'txtDateMonth' => $this->setDate($salaryInfo->ApplyDate, 'm') - 1,
                'txtDelete' => $salaryInfo->Del_Flag,
            );
        } else {
            $emp = array(
                'txtGrossSalary' => set_value('txtGrossSalary'),
                'txtApplyDate' => set_value('txtApplyDate'),
                'txtDateYear' => date('Y'),
                'txtDateMonth' => date('m') - 1,
                'txtDelete' => set_value('txtDelete'),
            );
        }

        $temp = array(
            'js' => array('jquery-ui.js'),
            'css' => array('jquery-ui.css'),
            'show' => 'add_salaryinfo',
            /* set label */
            'employeeName' => (empty($employee) ? '' : $employee->EmployeeName),
            'title' => (empty($title) ? 'Employee salary information Registration' : $title),
            'dialogTitle' => (empty($dialogTitle) ? 'Notification' : $dialogTitle),
            'back' => (empty($back) ? 'Back' : $back),
            'lblEmployeeName' => (empty($lblEmployeeName) ? 'Employee' : $lblEmployeeName),
            'lblGrossSalary' => (empty($lblGrossSalary) ? 'Gross Salary' : $lblGrossSalary),
            'lblUSD' => (empty($lblUSD) ? 'USD' : $lblUSD),
            'lblApplyDate' => (empty($lblApplyDate) ? 'Apply date' : $lblApplyDate),
            'lblDelete' => (empty($lblDelete) ? 'Delete' : $lblDelete),
            'submit' => (empty($lblSubmit) ? 'Submit' : $lblSubmit),
            'minYear' => $this->session->userdata('minYear'),
            'maxYear' => $this->session->userdata('maxYear'),
            /* set data */
            'employeeID' => $employeeID,
            'salaryInfoId' => $salaryInfoId,
        );
        return array_merge($temp, $emp);
    }

    /*
     * @author :        Quyet
     * @created:        23/6/2014
     * @Description:    basic data for new add salary info
     * @Input :         (Interger) $employeeID
     * @Input :         (Interger) $salaryInfoId
     * @Output :        Array
     * 
     */

    function getSalaryInforByID($employeeID = 0, $salaryInfoId = 0) {
        /* get salaryinfo */
        $condition = array(
            'select' => 'SalaryInfoId, GrossSalary, ApplyDate, a.Del_Flag as Del_Flag',
            'where' => array(
                'SalaryInfoId' => $salaryInfoId,
                'a.EmployeeId' => $employeeID,
                'b.CompanyCode' => $this->session->userdata('CompanyCode')
            ),
            'join' => array(
                array('table' => $this->table_employee . ' as b', 'condition' => 'a.EmployeeId = b.EmployeeId', 'type' => 'inner'),
            ),
            'first' => TRUE
        );
        return $this->selectDB($this->table_name . ' as a', $condition);
    }

}

?>
